package cn.syx.service.function;

import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.function.Function;

@Component
@Description("获取指定地点的当前星期几")
public class WeekService implements Function<WeekService.Request, WeekService.Response> {

    public record Request(@Schema(description = "地址") String address){

    }

    public record Response(String date){

    }

    @Override
    public Response apply(Request request) {
        String[] weekDays = {"日","一","二","三","四","五","六"};

        int w = getWeekOfDate();
        System.out.println(weekDays[w]);
        return new Response(String.format("%s的当天是周%s",request.address, weekDays[w]));

    }

    private int getWeekOfDate(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int w = cal.get(Calendar.DAY_OF_WEEK) -1;
        if(w < 0 ){
            w = 0;
        }
        return w;
    }



}
